using System;
using System.IO;
using System.Windows.Forms;
using GitProjectManager.Services;

namespace GitProjectManager.Models
{
    public partial class ProgramEditForm : Form
    {
        private readonly ProjectService? _projectService;
        private MainForm? _mainForm;

        public ProgramEditForm()
        {
            InitializeComponent();
        }

        public ProgramEditForm(ProjectService projectService, MainForm mainForm)
        {
            InitializeComponent();
            _projectService = projectService;
            _mainForm = mainForm;
            // 初始化列表显示
            UpdateProgramsList();
            
            // 设置为模态窗口
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterParent;
        }
        
        private void ProgramEditForm_Shown(object sender, EventArgs e)
        {
            // 窗口显示时自动聚焦
            this.Activate();
        }

        private void btnAddProgram_Click(object sender, EventArgs e)
        {
            try
            {
                // 创建一个简单的输入对话框
                Form inputForm = new Form();
                inputForm.Text = "添加自定义程序";
                inputForm.Width = 400;
                inputForm.Height = 200;
                inputForm.FormBorderStyle = FormBorderStyle.FixedDialog;
                inputForm.StartPosition = FormStartPosition.CenterParent;
                inputForm.MinimizeBox = false;
                inputForm.MaximizeBox = false;

                // 程序名称
                Label nameLabel = new Label();
                nameLabel.Text = "程序名称:";
                nameLabel.Location = new Point(20, 20);
                nameLabel.AutoSize = true;

                TextBox nameTextBox = new TextBox();
                nameTextBox.Location = new Point(120, 20);
                nameTextBox.Width = 250;

                // 程序路径
                Label pathLabel = new Label();
                pathLabel.Text = "程序路径:";
                pathLabel.Location = new Point(20, 50);
                pathLabel.AutoSize = true;

                TextBox pathTextBox = new TextBox();
                pathTextBox.Location = new Point(120, 50);
                pathTextBox.Width = 210;

                // 浏览按钮
                Button browseButton = new Button();
                browseButton.Text = "浏览...";
                browseButton.Location = new Point(335, 50);
                browseButton.Width = 35;
                browseButton.Click += (s, ev) =>
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    openFileDialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*";
                    openFileDialog.Title = "选择可执行文件";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        pathTextBox.Text = openFileDialog.FileName;
                    }
                };

                // 确认按钮
                Button okButton = new Button();
                okButton.Text = "确定";
                okButton.Location = new Point(200, 100);
                okButton.Width = 80;
                okButton.DialogResult = DialogResult.OK;

                // 取消按钮
                Button cancelButton = new Button();
                cancelButton.Text = "取消";
                cancelButton.Location = new Point(290, 100);
                cancelButton.Width = 80;
                cancelButton.DialogResult = DialogResult.Cancel;

                // 添加控件
                inputForm.Controls.Add(nameLabel);
                inputForm.Controls.Add(nameTextBox);
                inputForm.Controls.Add(pathLabel);
                inputForm.Controls.Add(pathTextBox);
                inputForm.Controls.Add(browseButton);
                inputForm.Controls.Add(okButton);
                inputForm.Controls.Add(cancelButton);

                inputForm.AcceptButton = okButton;
                inputForm.CancelButton = cancelButton;

                // 显示对话框
                if (inputForm.ShowDialog() == DialogResult.OK)
                {
                    string programName = nameTextBox.Text.Trim();
                    string programPath = pathTextBox.Text.Trim();

                    if (!string.IsNullOrEmpty(programName) && !string.IsNullOrEmpty(programPath))
                    {
                        if (File.Exists(programPath) || programPath.Contains(" "))
                        {
                            _projectService.AddExeProgram(programName, programPath);
                            UpdateProgramsList();
                            if (_mainForm != null)
                            {
                                _mainForm.UpdateProgramComboBox();
                            }
                            MessageBox.Show($"已添加程序: {programName}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("指定的程序文件不存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请输入程序名称和路径！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"添加程序失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEditProgram_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstPrograms.SelectedItem != null)
                {
                    string originalProgramName = lstPrograms.SelectedItem.ToString();
                    string originalPath = _projectService.GetExeProgramPath(originalProgramName);

                    // 创建编辑对话框
                    Form inputForm = new Form();
                    inputForm.Text = "修改自定义程序";
                    inputForm.Width = 400;
                    inputForm.Height = 200;
                    inputForm.FormBorderStyle = FormBorderStyle.FixedDialog;
                    inputForm.StartPosition = FormStartPosition.CenterParent;
                    inputForm.MinimizeBox = false;
                    inputForm.MaximizeBox = false;

                    // 程序名称
                    Label nameLabel = new Label();
                    nameLabel.Text = "程序名称:";
                    nameLabel.Location = new Point(20, 20);
                    nameLabel.AutoSize = true;

                    TextBox nameTextBox = new TextBox();
                    nameTextBox.Location = new Point(120, 20);
                    nameTextBox.Width = 250;
                    nameTextBox.Text = originalProgramName;

                    // 程序路径
                    Label pathLabel = new Label();
                    pathLabel.Text = "程序路径:";
                    pathLabel.Location = new Point(20, 50);
                    pathLabel.AutoSize = true;

                    TextBox pathTextBox = new TextBox();
                    pathTextBox.Location = new Point(120, 50);
                    pathTextBox.Width = 210;
                    pathTextBox.Text = originalPath;

                    // 浏览按钮
                    Button browseButton = new Button();
                    browseButton.Text = "浏览...";
                    browseButton.Location = new Point(335, 50);
                    browseButton.Width = 35;
                    browseButton.Click += (s, ev) =>
                    {
                        OpenFileDialog openFileDialog = new OpenFileDialog();
                        openFileDialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*";
                        openFileDialog.Title = "选择可执行文件";
                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            pathTextBox.Text = openFileDialog.FileName;
                        }
                    };

                    // 确认按钮
                    Button okButton = new Button();
                    okButton.Text = "确定";
                    okButton.Location = new Point(200, 100);
                    okButton.Width = 80;
                    okButton.DialogResult = DialogResult.OK;

                    // 取消按钮
                    Button cancelButton = new Button();
                    cancelButton.Text = "取消";
                    cancelButton.Location = new Point(290, 100);
                    cancelButton.Width = 80;
                    cancelButton.DialogResult = DialogResult.Cancel;

                    // 添加控件
                    inputForm.Controls.Add(nameLabel);
                    inputForm.Controls.Add(nameTextBox);
                    inputForm.Controls.Add(pathLabel);
                    inputForm.Controls.Add(pathTextBox);
                    inputForm.Controls.Add(browseButton);
                    inputForm.Controls.Add(okButton);
                    inputForm.Controls.Add(cancelButton);

                    inputForm.AcceptButton = okButton;
                    inputForm.CancelButton = cancelButton;

                    // 显示对话框
                    if (inputForm.ShowDialog() == DialogResult.OK)
                    {
                        string newProgramName = nameTextBox.Text.Trim();
                        string newProgramPath = pathTextBox.Text.Trim();

                        if (!string.IsNullOrEmpty(newProgramName) && !string.IsNullOrEmpty(newProgramPath))
                        {
                            if (File.Exists(newProgramPath) || newProgramPath.Contains(" "))
                            {
                                _projectService.UpdateExeProgram(originalProgramName, newProgramName, newProgramPath);
                                UpdateProgramsList();
                                if (_mainForm != null)
                                {
                                    _mainForm.UpdateProgramComboBox();
                                }
                                MessageBox.Show($"已更新程序: {originalProgramName} -> {newProgramName}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show("指定的程序文件不存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入程序名称和路径！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请先选择要修改的程序！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"修改程序失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRemoveProgram_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstPrograms.SelectedItem != null)
                {
                    string programName = lstPrograms.SelectedItem.ToString();
                    if (!string.IsNullOrEmpty(programName))
                    {
                        if (MessageBox.Show($"确定要移除程序 '{programName}' 吗？", "确认移除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            _projectService.RemoveExeProgram(programName);
                            UpdateProgramsList();
                            if (_mainForm != null)
                            {
                                _mainForm.UpdateProgramComboBox();
                            }
                            MessageBox.Show($"已移除程序: {programName}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请先选择要移除的程序！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"移除程序失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateProgramsList()
        {
            lstPrograms.Items.Clear();
            if (_projectService != null)
            {
                foreach (var program in _projectService.GetExePrograms())
                {
                    lstPrograms.Items.Add(program);
                }
            }
        }
    }
}